﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using NCLCustomerManagement.DataAccess;
using My.Common.Configuration;
using My.DataAccess.Core;

namespace NCLCustomerManagement.DataAccess.IocRegistration
{
    public class DataAccessModule : Module
    {
        private string _connectionString;

        

        public DataAccessModule()
        {
            _connectionString = GetConnectionString();
           
        }

        protected override void Load(ContainerBuilder builder)
        {
           
            RegisterUnitOfWork(builder);
            RegisterRepositories(builder);
        }

        private void RegisterUnitOfWork(ContainerBuilder builder)
        {
            builder.Register(c => new UnitOfWorkManager<NCLCustomerManagementDBDataContext>(_connectionString)).As<IUnitOfWork>();
        }
        
        private void RegisterRepositories(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(typeof(DataAccessModule).Assembly)
                .Where(t => t.Name.EndsWith("Repository"))
                .AsImplementedInterfaces()
                .PropertiesAutowired();
        }

        private string GetConnectionString()
        {
            var configuration = new AppConfigurationManager();
            return configuration.GetConnectStringSetting("DefaultConnection");
        }
    }
}
